<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/8/25
 * Time: 下午4:28
 */
class Get_video extends MY_Cron_Controller
{
    public function get()
    {
        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $this->load->model('direct_seeding/Model_direct_seeding_video_link', 'dsvl');

        $ds_data = $this->ds->lists(0, 10000, array());


        foreach ($ds_data as $v)
        {
            //production_   production_
            $anchor_data     = $this->get_video_link('9342_'.ENVIRONMENT.'_'.$v['ds_id'].'_'.DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_ANCHOR);
            $auction_data    = $this->get_video_link('9342_'.ENVIRONMENT.'_'.$v['ds_id'].'_'.DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_DEPUTY_ANCHOR);
            $confluence_data = $this->get_video_link('9342_'.ENVIRONMENT.'_'.$v['ds_id'].'_'.DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_CONFLUENCE);

            //p($v, $anchor_data, $auction_data);
            if (!empty($anchor_data['output']['file_list']))
            {
                $i = 1;
                foreach ($anchor_data['output']['file_list'] as $av)
                {
                    $duration = strtotime($av['end_time']) - strtotime($av['start_time']);
                    $this->dsvl->record($v['ds_id'], $av['record_file_url'], $duration, strtotime($av['start_time']), json_encode($av), DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_ANCHOR, $i, $av['stream_id']);
                    $i++;
                }
            }


            if (!empty($auction_data['output']['file_list']))
            {
                $i = 1;
                foreach ($anchor_data['output']['file_list'] as $av)
                {
                    $duration = strtotime($av['end_time']) - strtotime($av['start_time']);
                    $this->dsvl->record($v['ds_id'], $av['record_file_url'], $duration, strtotime($av['start_time']), json_encode($av), DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_DEPUTY_ANCHOR, $i, $av['stream_id']);
                    $i++;
                }
                //p($auction_data);
            }


            if (!empty($confluence_data['output']['file_list']))
            {
                $i = 1;
                foreach ($confluence_data['output']['file_list'] as $av)
                {
                    $duration = strtotime($av['end_time']) - strtotime($av['start_time']);
                    $this->dsvl->record($v['ds_id'], $av['record_file_url'], $duration, strtotime($av['start_time']), json_encode($av), DIRECT_SEEDING_PLAYBACK_VIDEO_TYPE_CONFLUENCE, $i, $av['stream_id']);
                    $i++;
                }
                //p($auction_data);
            }
        }
    }


    private function get_video_link($live_code)
    {
        //$key = 'f82b3d9040d4bb181d1e6e1a7b06cdb4';
        $key = '127d4fe7e9f253375ad9e60e9315b5f8';
        $t = strtotime('+1 days');//strtotime('2017-08-18 20:00:00');
        $appid = '1253714904';

        $sign = md5($key.$t);

        $url = "http://fcgi.video.qcloud.com/common_access?appid=$appid&interface=Live_Tape_GetFilelist&t=$t&sign=$sign&Param.s.channel_id=".$live_code;
        //$url .= "&Param.n.page_no=1&Param.n.page_size=50&Param.s.start_time=2017-07-01 00:00:00&Param.s.end_time=2017-08-15 00:00:00&Param.s.sort_type=desc";

        $data = array
        (
            //'Param.n.channel_id' => '498',
            'Param.n.page_no' => 1,
            'Param.n.page_size' => 50,
            //'t' => $t,
            //'sign' => $sign,
            'Param.s.start_time' => date('Y-m-d H:i:s', strtotime('-1 days')),
            'Param.s.end_time' => date('Y-m-d H:i:s', strtotime('+1 days')),
            'Param.s.sort_type' => 'desc',
        );

        //$str = http_build_query($data);p($url, $str);




        $data = json_encode($data);
        $data = array();
        //$data = http_build_query($data);
//echo $url.'<br/>';
        $rs = $this->_sendRequest($url, $data);
        return $rs;
        //p($rs, $data);
    }


    private function _sendRequest($url, $paramArray, $method = 'POST')
    {

        $ch = curl_init();

        if ($method == 'POST')
        {
            $paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
        }
        else
        {
            $url .= '?' . http_build_query($paramArray);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if (false !== strpos($url, "https")) {
            // 证书
            // curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false);
        }
        $resultStr = curl_exec($ch);

        $result = json_decode($resultStr, true);
        if (!$result)
        {
            return $resultStr;
        }
        return $result;
    }
}